package net.asfun.jvalog.control;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.common.DualCacher;
import net.asfun.jvalog.service.SiteService;
import net.asfun.jvalog.util.InputFilter;
import net.asfun.jvalog.util.ResponseUtil;
import net.asfun.jvalog.util.UpdateManager;
import net.asfun.jvalog.mvc.RequestMapping;

public class CacheController {

	
	@RequestMapping("/cache")
	public String index(HttpServletRequest req, Map<String, Object> model) {
		model.put("memoryStat", InputFilter.breakline(DualCacher.statMem()));
		model.put("dbstoreStat", InputFilter.breakline(DualCacher.statDb()));
		return "caches.html";
	}
	
	@RequestMapping("/cache/memclear")
	public String memClear(HttpServletRequest req, Map<String, Object> model) {
		DualCacher.clearMem();
		return ResponseUtil.feedback("cache.gmclear", req, model);
	}
	
	@RequestMapping("/cache/dbclear")
	public String dbClear(HttpServletRequest req, Map<String, Object> model) {
		DualCacher.clearDb();
		return ResponseUtil.feedback("cache.dbclear", req, model);
	}
	
	@RequestMapping("/cache/clear")
	public String clearAll(HttpServletRequest req, Map<String, Object> model) {
		SiteService.reset();
		SiteService.reCount();
		DualCacher.clearMem();
		DualCacher.clearDb();
		return ResponseUtil.feedback("cache.clear", req, model);
	}
	
	@RequestMapping("/cache/check")
	public String check(HttpServletRequest req, Map<String, Object> model) {
		if ( UpdateManager.updCounter ) {
			SiteService.reCount();
			UpdateManager.updCounter = false;
		}
		if ( UpdateManager.updStatic ) {
			SiteService.reset();
			UpdateManager.updStatic = false;
		}
		if ( UpdateManager.updCache ) {
			DualCacher.clearMem();
			DualCacher.clearDb();
			UpdateManager.updCache = false;
		}
		model.put("result", "ok");
		return "json";
	}
}
